define(function (require,exports,module) {
    var myPub = require('./public');
    var payPub = require('./submit_pay');

    var oUpLoadFile = $('.js_upLoadFile');                  // 上传文件input
    var oUpLoadStart = $('.js_upLoadStart');                // 未上传时显示
    var oUpLoadEnd = $('.js_upLoadEnd');                    // 上传时显示
    var oCloseBtn = $('.js_clearCloseBtn');                 // 清空按钮
    var oDownLoadLink = $('.js_downLoadLink');              // 下载模版按钮
    var oBatchTxt = $('.js_batchTxt');                      // 提示内容
    var oCarListBox = $('.js_carListBox');                  // 商品区域
    var $submitBtn = $(".js_btnPay");                      // 提交支付按钮
    var $orderForm = $('#superOrderForm');           // 需要支付的表单

    var lazLoading = new myPub.lazLoadingImg();
    lazLoading.AddLazLoadingImg(oCarListBox[0]);

    module.exports = {
        init:function () {
            userAgent();
        }
    };

    // 判断是设备
    function userAgent() {
        /*var userAgent = navigator.userAgent;
        console.log(userAgent);
        // 如果不是在电脑端
        if(userAgent.indexOf('Windows') === -1){

            // 提示框内提示
            oBatchTxt.text('请在电脑上打开');

            // 点击上传时弹窗提示
            oUpLoadFile.on('click',function (e) {
                // 取消默认行为
                e.preventDefault();
                myPub.TipLayer('请在电脑上打开','no',function () {
                    $('.layermbox').addClass('disbutor');
                })
            });

            // 点击下载模版时弹窗提示
            oDownLoadLink.on('click',function (e) {
                // 取消默认行为
                e.preventDefault();
                myPub.TipLayer('请在电脑上打开','no',function () {
                    $('.layermbox').addClass('disbutor');
                })
            })
        }else if(userAgent.indexOf('Windows') > -1){ */ // 如果是电脑端
            oUpLoadFile.val('');
            // 点击上传文件
            oUpLoadFile.on('change',function () {
                upFile();
            });
        /*}*/
    }

    $submitBtn.click(function(){
        var data = $orderForm.serialize();
        payPub.submitPay("/super/pay_super",data);
    });



    // 清除按钮
    oCloseBtn.on('click',function () {
        var _this = this;
        $(_this).hide();
        oUpLoadStart.show();
        oUpLoadEnd.hide();
        oUpLoadFile.val('');
        oDownLoadLink.show();
    });

    // 上传文件
    function upFile() {

        var oExcelFile = $('.js_upLoadFile').val();
        var FileName = oExcelFile.substring(oExcelFile.lastIndexOf('\\')+1,oExcelFile.length);

        // 当没有上传文件时
        if(oExcelFile == null || oExcelFile == ''){
            // 提示框内提示
            oBatchTxt.text('请选择将要上传的文件!');
            myPub.TipLayer('请选择将要上传的文件!','no',function () {
                $('.layermbox').addClass('disbutor');
            })
        }

        // 提示上传文件类型
        var oExcelExtend = oExcelFile.substring(oExcelFile.lastIndexOf('.'),oExcelFile.length);
        if(oExcelExtend === '.xls' || oExcelExtend === '.xlsx' || oExcelExtend === '.xlsm' || oExcelExtend === '.xlt' || oExcelExtend === '.xltx' || oExcelExtend === '.xltm'){

            oCloseBtn.show();
            oUpLoadStart.hide();
            oUpLoadEnd.show();
            oDownLoadLink.hide();
            oUpLoadEnd.find('.js_upLoadTxt').text(FileName);
            var form_data = new FormData();
            var file_data = $(".js_upLoadFile").prop("files")[0];
            var token = $(".js_token_excel").val();
            var invoiceExcel = $(".js_invoice_excel").val();
            form_data.append("invoice",invoiceExcel);
            form_data.append("token",token);
            form_data.append("file",file_data);
            // 在这里写交互
            $.ajax({
                type: "POST",
                url: "/super/excel_order",
                dataType : "json",
                processData: false,  // 注意：让jQuery不要处理数据
                contentType: false,  // 注意：让jQuery不要设置contentType
                data: form_data
            }).success(function(msg) {
                if (msg.status == 'success'){
                    $('.countNum').find('i').text(msg.obj[0]);
                    $('input[name=quantity]').val(msg.obj[0]);
                    $('.num').text('x' + msg.obj[0]);
                    $('.sumPrice').text((sumPrice * msg.obj[0]).toFixed(2));
                    myPub.TipLayer('导入成功 !上传了'+ msg.obj[1] +'订单，购买数量是'+ msg.obj[0] ,'no',function () {
                        $('.layermbox').addClass('disbutor');
                    });
                }else if (msg.status == 'empty'){
                    /*myPub.TipLayer( msg.message,'no',function () {
                        $('.layermbox').addClass('disbutor');
                    });*/
                    myPub.TipBtnLayer(msg.message,'no','确认',function () {
                        myPub.LayerCloseAll();
                    },function () {
                        
                    },function () {
                        $('.layermbox').addClass('disbutor');
                        $('.layermcont').addClass('argreeMentInfo batchTip');
                    });

                }else if (msg.status == 'fail'){
                    myPub.TipLayer( msg.message,'no',function () {
                        $('.layermbox').addClass('disbutor');
                    });
                } else {
                    myPub.TipLayer(msg.message,'no',function () {
                        $('.layermbox').addClass('disbutor');
                    })
                }
            }).fail(function(msg) {
            });
        }else{
            // 提示框内提示
            oBatchTxt.text('请选择正确的文件!');
            myPub.TipLayer('请选择正确的文件!','no',function () {
                $('.layermbox').addClass('disbutor');
            })
        }

    }

});